package com.example.bo.test4;

import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by BO on 2016-04-12.
 */
public class DownUtil {
    String path;
    String targetFile;
    int threadNum;
    DownThread[] thread;
    int filesize;
    public DownUtil(String path,String targetFil,int threadNum){
        this.path=path;
        this.targetFile=targetFil;
        this.threadNum=threadNum;
        thread = new DownThread[threadNum];
    }
    public void download()throws Exception{
        URL url=new URL(path);
        HttpURLConnection conn=(HttpURLConnection)url.openConnection();
        conn.setConnectTimeout(5 * 100);
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept","image/gif,image/jpeg,,*/*");
        conn.setRequestProperty("Accept-Language","zh-CN");
        conn.setRequestProperty("Connection","Keep-Alive");
        filesize=conn.getContentLength();
        int CurrentPartsize=filesize/threadNum+1;
        RandomAccessFile file=new RandomAccessFile(targetFile,"rw");

        file.setLength(filesize);
        file.close();
        for(int i=0;i<threadNum;i++){
            int starpos=i*CurrentPartsize;
            RandomAccessFile partfile =new RandomAccessFile(targetFile,"rw");
            partfile.seek(starpos);
            thread[i]=new DownThread(CurrentPartsize,starpos,partfile);
            thread[i].start();
        }

    }
    public double getcompleteRate(){
        int sumsize=0;
        for (int i=0;i<threadNum;i++){
            sumsize+=thread[i].length;
        }
        return sumsize*1.0/filesize;
    }
    private class DownThread extends Thread {
        int partsize;
        int startpos;
        private RandomAccessFile partfile;
        private int length=0;

        public DownThread(int partsize,int startpos,RandomAccessFile partfile){
            this.partsize=partsize;
            this.startpos=startpos;
            this.partfile=partfile;
        }

        @Override
        public void run() {
            try {
                URL url=new URL(path);
                HttpURLConnection conn=(HttpURLConnection)url.openConnection();
                conn.setConnectTimeout(5 * 100);
                conn.setRequestMethod("GET");
                conn.setRequestProperty("Accept", "image/gif,image/gif,image.,*/*");
                conn.setRequestProperty("Accept-Language", "zh-CN");

                InputStream inputStream=conn.getInputStream();
                inputStream.skip(this.startpos);
                byte[] buffer=new byte[1024];
                int hasread=0;
                while (length<partsize && (hasread=inputStream.read(buffer))>0){
                    partfile.write(buffer,0,hasread);
                    length+=hasread;
                }
                partfile.close();
                inputStream.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
